package logic

import (
	"context"
	"fmt"
	"math/rand"
	"meituan/cache"
	"meituan/pkg"
	"strconv"
	"time"

	"meituan/meituanrpc/internal/svc"
	"meituan/meituanrpc/meituanrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *meituanrpc.SendSmsRequest) (*meituanrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	i := rand.Intn(9000) + 1000

	sms, _ := pkg.SendSms(in.Mobile, strconv.Itoa(i))
	if sms.Code != 2 {
		return nil, fmt.Errorf("短信验证码获取失败")
	}

	cache.Rdb.Set(l.ctx, "sendsms"+in.Mobile, i, time.Minute*3)

	return &meituanrpc.SendSmsResponse{}, nil

}
